6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
16 protected function tearDown() {
17 $_SERVER = $this->oldServer
;
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
26 * @covers WebRequest::detectProtocol
28 public function testDetectServer( $expected, $input, $description ) {
29 $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
32 $result = WebRequest
::detectServer();
33 $this->assertEquals( $expected, $result, $description );
36 public static function provideDetectServer() {
51 'Host header with secure'
59 'Default SERVER_PORT',
73 'HTTP_X_FORWARDED_PROTO' => 'https',
82 'SERVER_PORT' => '81',
83 'HTTP_X_FORWARDED_PROTO' => 'https',
100 'Host server name precedence'
105 'HTTP_HOST' => '[::1]',
106 'SERVER_NAME' => '::1',
107 'SERVER_PORT' => '81',
114 'SERVER_NAME' => '[2001'
116 'Kind of like lighttpd per commit message in MW r83847',
119 'http://[2a01:e35:2eb4:1::2]:777',
121 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
123 'Possible lighttpd environment per bug 14977 comment 13',
128 protected function mockWebRequest( $data = [] ) {
129 // Cannot use PHPUnit getMockBuilder() as it does not support
130 // overriding protected properties afterwards
131 $reflection = new ReflectionClass( 'WebRequest' );
132 $req = $reflection->newInstanceWithoutConstructor();
134 $prop = $reflection->getProperty( 'data' );
135 $prop->setAccessible( true );
136 $prop->setValue( $req, $data );
142 * @covers WebRequest::getElapsedTime
144 public function testGetElapsedTime() {
145 $req = new FauxRequest();
146 $this->assertGreaterThanOrEqual( 0.0, $req->getElapsedTime() );
147 $this->assertEquals( 0.0, $req->getElapsedTime(), '', /*delta*/ 0.2 );
151 * @covers WebRequest::getVal
152 * @covers WebRequest::getGPCVal
153 * @covers WebRequest::normalizeUnicode
155 public function testGetValNormal() {
156 // Assert that WebRequest normalises GPC data using UtfNormal\Validator
157 $input = "a \x00 null";
158 $normal = "a \xef\xbf\xbd null";
159 $req = new FauxRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] );
160 $this->assertSame( $normal, $req->getVal( 'x' ) );
161 $this->assertNotSame( $input, $req->getVal( 'x' ) );
162 $this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) );
166 * @covers WebRequest::getVal
167 * @covers WebRequest::getGPCVal
169 public function testGetVal() {
170 $req = new FauxRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] );
171 $this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' );
172 $this->assertSame( null, $req->getVal( 'z' ), 'Not found' );
173 $this->assertSame( null, $req->getVal( 'y' ), 'Array is ignored' );
174 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
178 * @covers WebRequest::getRawVal
180 public function testGetRawVal() {
181 $req = new FauxRequest( [
186 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
187 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
188 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
189 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
193 * @covers WebRequest::getArray
195 public function testGetArray() {
196 $req = new FauxRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] );
197 $this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' );
198 $this->assertSame( null, $req->getArray( 'z' ), 'Not found' );
199 $this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) );
203 * @covers WebRequest::getIntArray
205 public function testGetIntArray() {
206 $req = new FauxRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] );
207 $this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' );
208 $this->assertSame( null, $req->getIntArray( 'z' ), 'Not found' );
209 $this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) );
213 * @covers WebRequest::getInt
215 public function testGetInt() {
216 $req = new FauxRequest( [
223 $this->assertSame( 0, $req->getInt( 'x' ), 'Text' );
224 $this->assertSame( 0, $req->getInt( 'y' ), 'Array' );
225 $this->assertSame( 0, $req->getInt( 'z' ), 'Not found' );
226 $this->assertSame( 0, $req->getInt( 'zero' ) );
227 $this->assertSame( 4, $req->getInt( 'answer' ) );
228 $this->assertSame( -2, $req->getInt( 'neg' ) );
232 * @covers WebRequest::getIntOrNull
234 public function testGetIntOrNull() {
235 $req = new FauxRequest( [
242 $this->assertSame( null, $req->getIntOrNull( 'x' ), 'Text' );
243 $this->assertSame( null, $req->getIntOrNull( 'y' ), 'Array' );
244 $this->assertSame( null, $req->getIntOrNull( 'z' ), 'Not found' );
245 $this->assertSame( 0, $req->getIntOrNull( 'zero' ) );
246 $this->assertSame( 4, $req->getIntOrNull( 'answer' ) );
247 $this->assertSame( -2, $req->getIntOrNull( 'neg' ) );
251 * @covers WebRequest::getFloat
253 public function testGetFloat() {
254 $req = new FauxRequest( [
261 $this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' );
262 $this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' );
263 $this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' );
264 $this->assertSame( 0.0, $req->getFloat( 'zero' ) );
265 $this->assertSame( 4.2, $req->getFloat( 'answer' ) );
266 $this->assertSame( -2.0, $req->getFloat( 'neg' ) );
270 * @covers WebRequest::getBool
272 public function testGetBool() {
273 $req = new FauxRequest( [
280 $this->assertSame( true, $req->getBool( 'x' ), 'Text' );
281 $this->assertSame( false, $req->getBool( 'y' ), 'Array' );
282 $this->assertSame( false, $req->getBool( 'z' ), 'Not found' );
283 $this->assertSame( false, $req->getBool( 'zero' ) );
284 $this->assertSame( true, $req->getBool( 'f' ) );
285 $this->assertSame( true, $req->getBool( 't' ) );
289 * @covers WebRequest::getFuzzyBool
291 public function testGetFuzzyBool() {
292 $req = new FauxRequest( [ 'x' => 'Value', 'f' => 'false', 't' => 'true' ] );
293 $this->assertSame( true, $req->getFuzzyBool( 'x' ), 'Text' );
294 $this->assertSame( false, $req->getFuzzyBool( 'z' ), 'Not found' );
295 $this->assertSame( false, $req->getFuzzyBool( 'f' ) );
296 $this->assertSame( true, $req->getFuzzyBool( 't' ) );
300 * @covers WebRequest::getCheck
302 public function testGetCheck() {
303 $req = new FauxRequest( [ 'x' => 'Value', 'zero' => '0' ] );
304 $this->assertSame( false, $req->getCheck( 'z' ), 'Not found' );
305 $this->assertSame( true, $req->getCheck( 'x' ), 'Text' );
306 $this->assertSame( true, $req->getCheck( 'zero' ) );
310 * @covers WebRequest::getText
312 public function testGetText() {
313 // FauxRequest overrides getText
314 $req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] );
315 $this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' );
319 * @covers WebRequest::getValues
321 public function testGetValues() {
322 $values = [ 'x' => 'Value', 'y' => '' ];
323 // FauxRequest overrides getValues
324 $req = $this->mockWebRequest( $values );
325 $this->assertSame( $values, $req->getValues() );
326 $this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' );
330 * @covers WebRequest::getValueNames
332 public function testGetValueNames() {
333 // FauxRequest overrides getValues
334 $req = new FauxRequest( [ 'x' => 'Value', 'y' => '' ] );
335 $this->assertSame( [ 'x', 'y' ], $req->getValueNames() );
336 $this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' );
340 * @dataProvider provideGetIP
341 * @covers WebRequest::getIP
343 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
345 $this->setMwGlobals( [
346 'wgSquidServersNoPurge' => $squid,
347 'wgUsePrivateIPs' => $private,
349 'IsTrustedProxy' => [
350 function ( &$ip, &$trusted ) use ( $xffList ) {
351 $trusted = $trusted ||
in_array( $ip, $xffList );
358 $request = new WebRequest();
359 $result = $request->getIP();
360 $this->assertEquals( $expected, $result, $description );
363 public static function provideGetIP() {
368 'REMOTE_ADDR' => '127.0.0.1'
378 'REMOTE_ADDR' => '::1'
388 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
389 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
391 [ 'ABCD:1:2:3:4:555:6666:7777' ],
399 'REMOTE_ADDR' => '12.0.0.1',
400 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
402 [ '12.0.0.1', '12.0.0.2' ],
405 'With X-Forwaded-For'
410 'REMOTE_ADDR' => '12.0.0.1',
411 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
416 'With X-Forwaded-For and disallowed server'
421 'REMOTE_ADDR' => '12.0.0.1',
422 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
427 'With multiple X-Forwaded-For and only one allowed server'
432 'REMOTE_ADDR' => '12.0.0.2',
433 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
435 [ '12.0.0.1', '12.0.0.2' ],
438 'With X-Forwaded-For and private IP (from cache proxy)'
443 'REMOTE_ADDR' => '12.0.0.2',
444 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
446 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
449 'With X-Forwaded-For and private IP (allowed)'
454 'REMOTE_ADDR' => '12.0.0.2',
455 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
457 [ '12.0.0.1', '12.0.0.2' ],
460 'With X-Forwaded-For and private IP (allowed)'
465 'REMOTE_ADDR' => '12.0.0.2',
466 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
468 [ '12.0.0.1', '12.0.0.2' ],
471 'With X-Forwaded-For and private IP (disallowed)'
476 'REMOTE_ADDR' => '12.0.0.1',
477 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
480 [ '12.0.0.1', '12.0.0.2' ],
482 'With X-Forwaded-For'
487 'REMOTE_ADDR' => '12.0.0.1',
488 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
493 'With multiple X-Forwaded-For and only one allowed server'
498 'REMOTE_ADDR' => '12.0.0.2',
499 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
504 'With X-Forwaded-For and private IP and hook (disallowed)'
509 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
510 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
512 [ 'ABCD:1:2:3::/64' ],
520 'REMOTE_ADDR' => '12.0.0.1',
521 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
532 * @expectedException MWException
533 * @covers WebRequest::getIP
535 public function testGetIpLackOfRemoteAddrThrowAnException() {
536 // ensure that local install state doesn't interfere with test
537 $this->setMwGlobals( [
538 'wgSquidServersNoPurge' => [],
539 'wgSquidServers' => [],
540 'wgUsePrivateIPs' => false,
544 $request = new WebRequest();
545 # Next call throw an exception about lacking an IP
549 public static function provideLanguageData() {
551 [ '', [], 'Empty Accept-Language header' ],
552 [ 'en', [ 'en' => 1 ], 'One language' ],
553 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
556 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
557 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
561 [ 'es' => 1, 'en' => '0.5' ],
562 'Spanish as first language and English and second'
564 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
565 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
566 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
567 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
569 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
570 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
571 'Preference for Romance languages'
575 [ 'en-gb' => 1, 'en-us' => '1' ],
576 'Two equally prefered English variants'
578 [ '_', [], 'Invalid input' ],
583 * @dataProvider provideLanguageData
584 * @covers WebRequest::getAcceptLang
586 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
587 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
588 $request = new WebRequest();
589 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );